package com.surelution.core.drivers.bluecard;

import java.util.Properties;

import com.surelution.core.drivers.Connection;
import com.surelution.core.drivers.Wand;

public class BluecardConnection implements Connection {

	private String port;

	public boolean open() {
		BlueCardLibrary library = BlueCardLibraryFactory.create();
		int ret = library.Term_CommOpen(port);
		return ret == 1;
	}
	
	public boolean close() {
		BlueCardLibrary library = BlueCardLibraryFactory.create();
		int ret = library.Term_CommClose();
		return ret == 1;
	}
	
	public Station prepareStation() {
		Station station = null;
		BlueCardLibrary library = BlueCardLibraryFactory.create();
		int[] serialNo = new int[1], stationID = new int[1];
		int ret = library.StationCall(serialNo, stationID);
		if(ret == 1) {
			station = new Station(serialNo[0], stationID[0]);
		}
		return station;
	}
	
	public Wand prepareWand() {
		Wand wand = null;
		BlueCardLibrary library = BlueCardLibraryFactory.create();
		int[] termId = new int[1];
		int[] serialNo = new int[1];
		short ret = library.Term_CommCall(termId, serialNo);
		if(ret == 1) {
			wand = new BluecardWand(termId[0], serialNo[0]);
		}
		return wand;
	}

	public void setProperties(Properties prop) {
		if(prop != null) {
			String port = (String)prop.get("port");
			this.port = port;
		}
	}
}
